package com.gitee.wsl.compose.use


import androidx.compose.runtime.State
import arrow.core.Either
import arrow.core.left
import arrow.core.right
import kotlinx.coroutines.CoroutineScope


internal typealias IsActive = State<Boolean>

internal typealias SuspendAsyncFn = suspend CoroutineScope.() -> Unit

internal typealias SetValueFn<T> = (T) -> Unit
internal typealias ResetFn = () -> Unit
internal typealias GetValueFn<T> = () -> T

internal typealias PauseFn = () -> Unit
internal typealias ResumeFn = () -> Unit

internal typealias OnEndCallback = () -> Unit

/**
 * 优化函数调用，通过使用[Either]，可以实现在kotlin平台的解构多态，可以实现如下代码，增加灵活性
 * ```kotlin
 * import xyz.junerver.compose.hooks.invoke
 * // before
 * set(3.left())
 * set({value:Int ->
 *      value/3
 * }.right())
 * // after
 * set(3)
 * set { value: Int -> value / 3 }
 * ```
 *
 * 但是必须要明确Either的泛型类型，不能直接使用泛型
 */
internal typealias SetterEither<T> = Either<T, (T) -> T>

operator fun SetValueFn<SetterEither<Int>>.invoke(leftValue: Int) = this(leftValue.left())

operator fun SetValueFn<SetterEither<Int>>.invoke(rightValue: (Int) -> Int) = this(rightValue.right())

/**
 * 退化函数调用，将`SetValueFn<SetterEither<T>>`转换为`SetValueFn<T>`，方便使用
 */
fun <T> SetValueFn<SetterEither<T>>.left(): SetValueFn<T> = { leftValue -> this(leftValue.left()) }

